package com.etime.shycourse.controller;

import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shy.common.PojoMessage;
import com.etime.shycourse.pojo.Chapter;
import com.etime.shycourse.services.interfaces.ChapterServices;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wb
 * @since 2021-12-21
 */
@Api(tags = "章节管理数据接口")
@RestController
@RequestMapping("/sys/chapter")
public class ChapterController {
    @Autowired
    private ChapterServices service;

    /**
     * selectChapterAllByCourseId
     *
     * @param courseId
     * @return List<Chapter>
     */
    @ApiOperation("根据课程id查询课程所有的章节(只有章) courseid ")
    @ApiImplicitParam(name = "courseId", value = "课程id")
    @GetMapping("/list/{courseId}")
    public ListMessage<Chapter> selectChapterAllByCourseId(@PathVariable Integer courseId) {
        List<Chapter> data = service.selectChapterAllByCourseId (courseId);
        ListMessage<Chapter> msg = new ListMessage<> ();
        if (data != null) {
            msg.setCode (1);
            msg.setDatalist (data);
        } else {
            msg.setCode (0);
            msg.setMsg ("没有指定课程的章节");
        }
        return msg;
    }

    @ApiOperation("增加课程章功能")
    @ApiImplicitParam("创建一个章 不用传chapterpid 默认-1")
    @PostMapping("/insert/one")
    public PojoMessage<Chapter> insertChapter(Chapter chapter, HttpServletRequest request) {
        String token = request.getHeader("token");
        //章的父id ==-1 	节的父id==章的id chapterpid 默认-1
        Chapter chapters = service.insertChapter(chapter, token);
        PojoMessage<Chapter> msg = new PojoMessage<>();
        if (chapters != null) {
            msg.setCode(1);
            msg.setData(chapters);
        } else {
            msg.setCode(0);
            msg.setMsg("添加课程章失败");
        }
        return msg;
    }

    @ApiOperation("增加、修改课程节功能")
    @ApiImplicitParam("1、创建一个节;2、修改章、节时,必传值:chapterid;3、lessons[0].*都不传")
    @PostMapping("/insert/two")
    public BaseMessage insertChapterFestival(Chapter chapter) {
        int rtn = service.insertChapterFestival (chapter);
        BaseMessage msg = new BaseMessage ();
        msg.setCode (rtn);
        if (rtn > 0) {
            msg.setMsg ("增加或修改章节成功");
        } else {
            msg.setCode (0);
            msg.setMsg ("增加或修改章节失败");
        }
        return msg;
    }
}

